from sanic import Sanic
from sanic.response import text, json
from entitys.BaiduEntity import BaiduData
from entitys.KeyWordsEntity import KeyWords
from datetime import datetime

app = Sanic("baidu")


@app.get("/getList")
async def getList(request):
    result = []
    list = BaiduData.getList()
    for i in list:
        addTime = datetime.fromtimestamp(float(i[10])).strftime("%Y-%m-%d %H:%M:%S")
        baidu = BaiduData(i[1], i[2], i[3], i[4], i[5], i[7], i[6], i[9], addTime, i[0])
        result.append(baidu.__dict__)
    return json({"code": 200, "data": result, "mgs": "success"})


@app.post("/add")
async def add(request):
    data = request.json
    # 定义一个KeyWords对象
    data = KeyWords(data["keyword"], datetime.now(), datetime.now())
    KeyWords.add(data)
    return json({"code": 200, "mgs": "添加成功~"})


if __name__ == "__main__":
    app.run(host="127.0.0.1", port=8000)
